home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoyc01.zip / MODEDLG.CPP < prev    next >
C/C++ Source or Header  |  1994-01-12  |  8KB  |  373 lines

  1. /***************************************************************************
  2.   ModeDialog unit
  3.   A dialog displaying available video modes, supporting routines
  4.   PJB August 30, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   If you want to omit certain video modes from the list, change the
  10.   AddMode void to include a test (e.g. if Columns<80 ) Exit...)
  11.  
  12.   Turbo Vision works in 40 columns, but the SelectVideoMode dialog does
  13.   not (it is too wide, selecting Preview will shrink the dialog).
  14.  
  15.   You can overlay this unit and put TSelectVideoModeDialog in a
  16.   resource file. Here is what to do with a resource file:
  17.  
  18.     TVideoList::setup();
  19.     selectVideoMode(PSelectVideoModeDialog(RezFile.get('VideoModeDialog')));
  20.  
  21.  
  22.   See VIDEOTST for a demonstration of this unit.
  23.  
  24. ***************************************************************************/
  25.  
  26. #define Uses_TApplication
  27. #define Uses_TEvent
  28. #define Uses_TMouse
  29. #define Uses_TScreen
  30. #define Uses_TStringCollection
  31. #include <tv.h>
  32. #include <strstream.h>
  33. #include <iomanip.h>
  34.  
  35. #include <math.h>
  36. #include "toycfg.h"
  37. #include "modedlg.h"
  38. #include "tvvideo.h"
  39. #include "vesa.h"
  40. #include "video.h"
  41.  
  42.  
  43. const
  44.   cmPreview = 1000,
  45.   cmRescan  = 1001;
  46.  
  47.  
  48. // SelectVideoModeDialog GetData/SetData operates on this
  49. struct {
  50.   TListBoxRec videoListBox;
  51. } videoModeDataRec;
  52.  
  53.  
  54. //
  55. //  Delay for Ticks 18ths of a second, calling Idle
  56. //
  57.  
  58. void wait(ushort ticks)
  59. {
  60.   ushort finish;
  61.  
  62.   finish=MemW(Seg0040,0x6C)+ticks;
  63.   while (finish-MemW(Seg0040,0x6C)<=ticks)
  64.     TApplication::application->idle();
  65. }
  66.  
  67.  
  68. //
  69. //  This procedure will be called by scanEVGAModes with
  70. //  new mode information.
  71. //
  72.  
  73. void far TVideoList::addMode(ushort mode, ushort rows, ushort columns,
  74.                  ushort charHeight, Boolean color)
  75. {
  76.   if (columns>=80 && TVideoList::count<maxVideoModes)
  77.   {
  78.     modes[count].mode=mode;
  79.     modes[count].columns=columns;
  80.     modes[count].rows=rows;
  81.     modes[count].charHeight=charHeight;
  82.     modes[count].color=color;
  83.     count++;
  84.  
  85.  
  86.     char        str[80];
  87.     ostrstream  line(str, sizeof str);
  88.  
  89.     line
  90.       << setw(3) << hex << mode << "h  "
  91.       << setw(3) << dec << columns
  92.       << "x"
  93.       << rows
  94.       << "  "
  95.       << setw(2) << charHeight << "p"
  96.       << "  ";
  97.  
  98.     if (mode==TDisplay::smBW80)
  99.       line << "b/w";
  100.     else
  101.       if (color)
  102.         line << "color";
  103.       else
  104.         line << "mono";
  105.  
  106.     line << (char)0;
  107.     list->insert(newStr(str));
  108.   }
  109. }
  110.  
  111.  
  112.  
  113. int TVideoList::count=0;
  114. Boolean TVideoList::hasToScan=True;
  115. TStringCollection *TVideoList::list=0;
  116. ModeDataRec TVideoList::modes[maxVideoModes];
  117. const char * const TVideoList::name = "TVideoList";
  118.  
  119.  
  120. //
  121. //  Simple example of how to find a reasonably similar video mode
  122. //  Tries to weigh width and height differently.
  123. //
  124.  
  125. ushort TVideoList::findSimilarVideoMode(uchar columns, uchar rows, Boolean color)
  126. {
  127.   ushort diff;
  128.   ushort oldDiff=999;
  129.   ushort mode=TScreen::screenMode;
  130.  
  131.   for (int i=0; i<count; i++)
  132.   {
  133.     diff=abs(modes[i].rows-rows)+
  134.           abs(modes[i].columns-columns)/2 +
  135.           20*(ushort)(modes[i].color!=color);
  136.  
  137.     if (diff<oldDiff)
  138.     {
  139.       oldDiff=diff;
  140.       mode=modes[i].mode;
  141.     }
  142.   }
  143.   return mode;
  144. }
  145.  
  146.  
  147. //
  148. //  Scan for video modes and add to videoList
  149. //
  150.  
  151. void TVideoList::setup()
  152. {
  153.   if (hasToScan)    // Check for previous list...
  154.   {
  155.     hasToScan=False;
  156.     list = new TStringCollection(20,10);
  157.  
  158.    #ifdef TOYVESASUPPORT
  159.     if (vesaScanningPossible())
  160.     {
  161.       //
  162.       //  Add standard modes if necessary, Marek Bojarski's idea
  163.       //
  164.       if (!standardInfoAvailable)
  165.       {
  166.         TMouse::hide();
  167.         scanEVGAModes(0, standardTextModes, addMode);
  168.         setSpecialScreenMode(TScreen::screenMode);
  169.         TMouse::show();
  170.       }
  171.       scanVesaModes(addMode);
  172.     }
  173.     else
  174.    #endif
  175.     {
  176.       TMouse::hide();
  177.  
  178.       scanEVGAModes(0, vgaModes, addMode);
  179.  
  180.      #ifdef TOYVESASUPPORT   // If not vesaScanningPossible
  181.       if (vesaVersion!=0)
  182.         scanEVGAModes(0x100, vesaModes, addMode);
  183.      #endif
  184.  
  185.       // Restore Turbo Vision screen
  186.       setSpecialScreenMode(TScreen::screenMode);
  187.       TMouse::show();
  188.     }
  189.   }
  190.   videoModeDataRec.videoListBox.collection=list;
  191. }
  192.  
  193.  
  194. //
  195. //  These routines save mode information on a stream. They are meant
  196. //  to be used with an init or configuration file
  197. //
  198.  
  199. void TVideoList::write(opstream& os)
  200. {
  201.   os << list;
  202.   os.writeBytes(&modes, sizeof(modes));
  203. }
  204.  
  205. void *TVideoList::read(ipstream& is)
  206. {
  207.   is >> list;
  208.   is.readBytes(&modes, sizeof(modes));
  209.   return this;
  210. }
  211.  
  212. TStreamable *TVideoList::build()
  213. {
  214.   return new TVideoList(streamableInit);
  215. }
  216.  
  217.  
  218. //
  219. //  Let the user select a video mode
  220. //
  221.  
  222. void selectVideoModeDialog()
  223. {
  224.   selectVideoMode(new TSelectVideoModeDialog());
  225. }
  226.  
  227.  
  228. static ushort execDialog(TWindow *D, void *p=0)
  229. {
  230.   if (p)
  231.     D->setData(p);
  232.  
  233.   ushort cm=TApplication::application->execView(D);
  234.  
  235.   if (p)
  236.     D->getData(p);
  237.  
  238.   return cm;
  239. }
  240.  
  241.  
  242.  
  243. //
  244. //  Dialog already created, now execute it
  245. //
  246.  
  247. void selectVideoMode(TSelectVideoModeDialog *d)
  248. {
  249.   for (int i=0; i<TVideoList::count; i++)
  250.     if (TVideoList::modes[i].mode==TScreen::screenMode)
  251.       videoModeDataRec.videoListBox.focused=i;
  252.  
  253.   if (execDialog(d, &videoModeDataRec)==cmOK)
  254.     if (TVideoList::count>0)
  255.       setSpecialScreenMode(TVideoList::modes[videoModeDataRec.videoListBox.focused].mode);
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262. //
  263. //   The mode dialog
  264. //
  265.  
  266. TSelectVideoModeDialog::TSelectVideoModeDialog() :
  267.        TDialog(TRect(14, 7, 66, 24), "Select Video Mode"),
  268.        TWindowInit(TSelectVideoModeDialog::initFrame)
  269.  
  270. {
  271.  TView *control;
  272.  options |= ofCentered;
  273.  
  274.  control = new TScrollBar(TRect(32, 3, 33, 15));
  275.  insert(control);
  276.  
  277.  videoListBox = new TListBox(TRect(5, 3, 32, 15), 1, (TScrollBar*)control);
  278.  insert(videoListBox);
  279.  
  280.    insert(new TLabel(TRect(4, 2, 16, 3), "~V~ideo modes", videoListBox));
  281.  
  282.  control = new TButton(TRect(37, 3, 48, 5), "~P~review", cmPreview, bfDefault);
  283. // control->helpCtx = hcPreview;
  284.  insert(control);
  285.  
  286.  control = new TButton(TRect(37, 6, 48, 8), "O~K~", cmOK, bfNormal);
  287. // control->helpCtx = hcOK;
  288.  insert(control);
  289.  
  290.  control = new TButton(TRect(37, 11, 48, 13), "~R~escan", cmRescan, bfNormal);
  291. // control->helpCtx = hcRescan;
  292.  insert(control);
  293.  
  294.  control = new TButton(TRect(37, 8, 48, 10), "Cancel", cmCancel, bfLeftJust);
  295. // control->helpCtx = hcCancel;
  296.  insert(control);
  297.  
  298.  control = new TButton(TRect(37, 14, 48, 16), "Help", cmHelp, bfNormal);
  299. // control->helpCtx = hcSelectVideoMode;
  300.  insert(control);
  301.  
  302.  selectNext(False);
  303. }
  304.  
  305.  
  306. //
  307. //  Events
  308. //
  309.  
  310. void TSelectVideoModeDialog::handleEvent(TEvent& event)
  311. {
  312.   TDialog::handleEvent(event);
  313.  
  314.   if (event.what & evMessage)
  315.   {
  316.     switch (event.message.command)
  317.     {
  318.       case cmListItemSelected:      /* Mouse double clicked in list */
  319.       case cmPreview:
  320.       {
  321.         ushort oldMode=TScreen::screenMode;
  322.         setSpecialScreenMode(TVideoList::modes[videoListBox->focused].mode);
  323.         wait(previewTime);
  324.         setSpecialScreenMode(oldMode);
  325.     break;
  326.       }
  327.       case cmRescan:
  328.         TVideoList::hasToScan=True;
  329.         TVideoList::list=0;
  330.         TVideoList::setup();
  331.         videoListBox->newList(TVideoList::list);
  332.     break;
  333.       default:
  334.     return;
  335.     }
  336.     clearEvent(event);
  337.   }
  338. }
  339.  
  340. const char * const TSelectVideoModeDialog::name = "TSelectVideoModeDialog";
  341.  
  342. void TSelectVideoModeDialog::write( opstream& os )
  343. {
  344.  TDialog::write( os );
  345.  os << videoListBox;
  346. }
  347.  
  348. void *TSelectVideoModeDialog::read( ipstream& is )
  349. {
  350.  TDialog::read( is );
  351.  is >> videoListBox;
  352.  return this;
  353. }
  354.  
  355. TStreamable *TSelectVideoModeDialog::build()
  356. {
  357.     return new TSelectVideoModeDialog( streamableInit );
  358. }
  359.  
  360. // From here to end of file may be removed if TSelectVideoModeDialog will not be streamed.
  361.  
  362. TStreamableClass RModeDialog( TSelectVideoMo